/**
 * 
 */
package com.ctg.payroll2.action.cusadmin;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.ctg.payroll2.bean.po.SysUser;
import com.ctg.payroll2.exception.AccountHasExistedException;
import com.ctg.payroll2.service.cusadmin.CsAccountService;
import com.ctg.payroll2.service.cusadmin.PbCompanyService;
import com.ctg.payroll2.util.Constants;
import com.ctg.platform.core.security.SpringSecurityManager;
import com.ctg.platform.util.Page;
import com.ctg.platform.util.Parse;
import com.opensymphony.xwork2.ActionSupport;

/**
 * @author 郑致力
 *
 */
public class CusAdminAction extends ActionSupport{
	/**
	 * 
	 */
	private static final long serialVersionUID = -1979594724905036737L;
	private Page result;
	private String msg;
	private boolean success;
	
	
	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public boolean isSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

	public Page getResult() {
		return result;
	}

	public void setResult(Page result) {
		this.result = result;
	}
	
	/**
	 * 校验账户是否已存在
	 * 
	 * @return
	 */
	public String validateAccount() {
		String username = null;
		
		HttpServletRequest request = ServletActionContext.getRequest();
		username = Parse.toString(request.getParameter("_user_code"), false);
		
		WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletActionContext.getServletContext());
		SpringSecurityManager springSecurityManager = (SpringSecurityManager) wac.getBean("springSecurityManager");
		if (springSecurityManager.getAccountByUserName(username, null) == null) {
			this.msg = "true";
		} else {
			this.msg = "false";
		}
		this.success = true;
		return SUCCESS;
	}

	/**
	 * 添加客户系统管理员
	 * 
	 * @return
	 */
	public String addSystemAdmin() {
		String username = null;
		String password = null;
		String displayName = null;
		
		HttpServletRequest request = ServletActionContext.getRequest();
		username = Parse.toString(request.getParameter("_user_code"), false);
		password = Parse.toString(request.getParameter("_user_pwd"), false);
		displayName = Parse.toString(request.getParameter("_user_name"), false);
		
		SysUser user = (SysUser)request.getSession().getAttribute(Constants.CURRENT_USER);
		
		WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletActionContext.getServletContext());
		CsAccountService csAccountService = (CsAccountService) wac.getBean("csAccountService");	
		try {		
			csAccountService.addSystemAdminAccount(username, password, displayName, user);
		} catch (AccountHasExistedException ahee) {
			msg = "AccountHasExistedException";
		} catch (Exception e) {
			e.printStackTrace();
			this.success = false;
			return ERROR;
		}
		this.success = true;
		return SUCCESS;
	}
	
	public String addClientSystemAdmin() {
		String company_code = null;
		String username = null;
		String password = null;
		String displayName = null;
		
		HttpServletRequest request = ServletActionContext.getRequest();
		company_code = Parse.toString(request.getParameter("_company_code"), false);
		username = Parse.toString(request.getParameter("_user_code"), false);
		password = Parse.toString(request.getParameter("_user_pwd"), false);
		displayName = Parse.toString(request.getParameter("_user_name"), false);
		
		SysUser user = (SysUser)request.getSession().getAttribute(Constants.CURRENT_USER);
		
		WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletActionContext.getServletContext());
		CsAccountService csAccountService = (CsAccountService) wac.getBean("csAccountService");	
		PbCompanyService pbCompanySevice=(PbCompanyService) wac.getBean("companyService");
		try {		
			csAccountService.addClientSystemAdminAccount(company_code, username, password, displayName, user);
			pbCompanySevice.updateStatus(company_code);
		} catch (AccountHasExistedException ahee) {
			msg = "AccountHasExistedException";
		} catch (Exception e) {
			e.printStackTrace();
			this.success = false;
			return ERROR;
		}
		this.success = true;
		return SUCCESS;		
	}
	
	/**
	 * 分页查询系统管理员和客户系统管理员
	 * 
	 * @return
	 */
	public String queryAdminSystem(){
		String user_code = null;
		String company_chinese_code = null;
		Boolean isSystemAdmin = null;
		Boolean isFreeze = null;
		Integer start = null;
		Integer limit = null;
		
		HttpServletRequest request = ServletActionContext.getRequest();
		user_code = Parse.toString(request.getParameter("user_code"),false);
		company_chinese_code = Parse.toString(request.getParameter("company_chinese_code"),false);
		if (request.getParameter("role") == null || request.getParameter("role").trim().length() != 0) {
			isSystemAdmin = Parse.toBoolean(request.getParameter("role"));	
		}
		if (request.getParameter("isFreeze") == null || request.getParameter("isFreeze").trim().length() != 0) {
			isFreeze = Parse.toBoolean(request.getParameter("isFreeze"));
		}
		start = Parse.toInteger(request.getParameter("start") , 0);
		limit = Parse.toInteger(request.getParameter("limit"), Constants.DEFAULT_PAGE_SIZE);
		
		WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletActionContext.getServletContext());
		CsAccountService csAccountService = (CsAccountService) wac.getBean("csAccountService");
		result = csAccountService.queryWithPaginationByAdmin(user_code, company_chinese_code, isSystemAdmin, isFreeze, start, limit);
		this.success = true;
		return SUCCESS;
	}
	
	/**
	 * 系统管理员修改的账户信息
	 * 
	 * @return
	 */
	public String modifyAccountByAdmin() {
		String user_id = null;
		String username = null;
		
		HttpServletRequest request = ServletActionContext.getRequest();
		user_id = Parse.toString(request.getParameter("_user_id_modify"), false);
		username = Parse.toString(request.getParameter("_user_name_modify"), false);
		
		WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletActionContext.getServletContext());
		CsAccountService csAccountService = (CsAccountService) wac.getBean("csAccountService");
		try {
			csAccountService.modifyAdminAccount(user_id, username);
		} catch (Exception e) {
			this.success = false;
			return SUCCESS;
		}
		this.success = true;
		return SUCCESS;
	}
}
